package pprofile

import (
	"net/http"
	_ "net/http/pprof"
	"os"
	"runtime/pprof"
	"sync"
)

var once sync.Once

func Setup() {
	once.Do(func() {
		// 启动HTTP服务器以便于web界面查看性能数据
		go func() {
			http.ListenAndServe("localhost:6060", nil)
		}()

		// 创建CPU profile文件
		f, err := os.Create("cpu.prof")
		if err != nil {
			panic(err)
		}
		pprof.StartCPUProfile(f)
	})
}

func Stop() {
	pprof.StopCPUProfile()
}
